package cn.loan.controller.exception;

import cn.loan.config.key.RequestKey;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 毛苏宇
 * @Description: springmvc异常处理类
 * @Date: Create in 18:49 2019/4/29
 */
@ControllerAdvice
public class ExceptionController {

    /**
     * 404找不到页面时
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public String notFound(NoHandlerFoundException e, HttpServletRequest request) {
        //得到错误摘要
        String message = e.getMessage();
        //将错误存储至request中,携带到前端
        request.setAttribute(RequestKey.REQUEST_NOT_FOUND, message);
        //404是视图名
        return "/error/404";
    }

}
